<?php
namespace Plu\Library\Models\Platform\Media;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column,
    Phalcon\Mvc\Model\Resultset\Simple as Resultset;


class ReplayMedia extends Model {
    // 回看视频编号
    public $Id;
    // 所属房间  
    public $RoomId;
    // 视频编号
    public $VideoId;
    // 场次ID
    public $LiveId;
    // 所属游戏编号
    public $GameId;
    // 用户ID
    public $UserId;
    // 标题
    public $Title;
    // 播放次数
    public $PlayTimes;
    // 回看视频状态
    public $ReplayVideoStatus;
    // 删除状态 
    public $Status;
    // 举报次数 
    public $ReportedTimes;
    // 举报用户
    public $ReportUsers;
    // 举报原因
    public $ReportReasons;
    // 创建时间
    public $CreateTime;
    // 最后修改时间
    public $LastModified;

    const ReplayVideoStatus = [
        'Examine'   =>  0,  // 初始状态
        'pass'      =>  1,  // 审核通过
        'refuse'    =>  2   // 审核未通过
    ];

    const ReplayFilter = [
        'Examine' => ' = 0',
        'pass'    => ' = 1',
        'refuse'  => ' = 2' 
    ];

    public function initialize() {
        $this->setReadConnectionService('db.platform.slave');
        $this->setWriteConnectionService('db.platform');
        $this->allowEmptyStringValues(['ReportUsers', 'ReportReasons']); 
    }

    public function getSource() {
        return 'ReplayVideoDisplay';
    }

    public function getVideos($offset, $limit, $filter, $uploadtime_start = null, $uploadtime_end = null, $title, $userid) {
        $condition = [];
        $bind = [];

        // uploadtime search
        if ($uploadtime_start) {
            $startTime = date('Y-m-d', strtotime($uploadtime_start));
            $condition[] = " CreateTime > :start: ";
            $bind['start'] = $startTime;
        }

        if ($uploadtime_end) {
            $endTime = date('Y-m-d', (strtotime($uploadtime_end) + 86400));
            $condition[] = " CreateTime < :end: ";
            $bind['end'] = $endTime;
        }

        // Deleted Status
        $condition[] = "Status !=-1";

        // title search
        if ($title) {
            $condition[] = " Title = :title: ";
            $bind['title'] = $title;
        }
        // userid search
        if ($userid) {
            $condition[] = " UserId = :userId: ";
            $bind['userId'] = $userid;
        }

        $condition[] = " ReplayVideoStatus = :status: ";
        $bind['status'] = self::ReplayVideoStatus[$filter];

        $condition = join(" AND ", $condition);

        $where = [
            'conditions' => $condition,
            'bind'  => $bind
        ];

        $count = self::count($where);

        $where['order'] = "Id DESC";
        $where['limit'] = $limit;
        $where['offset'] = $offset;
        
        $items = self::find($where);
  
        if ($items) {
            $items = $items->toArray();
        } else {
            $items = [];
        }

        $res = [$count, $items];

        return $res;
    }

    static public function getVideo($videoId, $isArray = true) {

        $where = [
            'Id = :id:',
            'bind' => [
                'id' => $videoId
            ]
        ];

        $item = self::findFirst($where);

        if ($item) {
            if ($isArray) {
                $item = $item->toArray();
            }
            return $item;
        }

        return false;

    }


    static public function refuseVideo($mediaId, $managerId) {
        $where = [
            'Id = :id:',
            'bind' => [
                'id' => $mediaId
            ]
        ];

        $item = self::findFirst($where);

        $item->ReplayVideoStatus = self::ReplayVideoStatus['refuse'];
        $item->LastModified  = date("Y-m-d H:i:s");

        if ($item->save()) {
            return true;
        }

        return false;
    }

    static public function passVideo($mediaId, $managerId) {
        $where = [
            'Id = :id:',
            'bind' => [
                'id' => $mediaId
            ]
        ];

        $item = self::findFirst($where);

        $item->ReplayVideoStatus = self::ReplayVideoStatus['pass'];
        $item->LastModified  = date("Y-m-d H:i:s");

        if ($item->update()) {
            return true;
        } else {
            $msg = $item->getMessages();
            print_r($msg);
        } 
        return false;
    }

    static public function updateTitle($mediaId, $title) {
        $where = array(
            'Id = :id:',
            'bind'  =>  array(
                'id'    =>  $mediaId,
            ),
        );

        $item = self::findFirst($where);
        $item->Title = $title;
        $res = $item->update();
        if ($res) {
            return $item;
        }
        return false;
    }

    static public function UpdateTitleByLiveId($LiveId, $title) {
        //date_default_timezone_set('Asia/Shanghai');
        $where = array(
            'LiveId = :id:',
            'bind'  =>  array(
                'id'    =>  $LiveId,
            ),
        );
        $item = self::findFirst($where);
        if ($item) {
            $item->Title = $title;
            $res = $item->save();
        }
        return true;
    }
}
?>
